<?php
namespace app\index\controller\admin;

use app\common\controller\Back;

class Dub extends Back
{
    private $defaults = [
        'id' => 4,
    ];

    protected function initialize()
    {
        parent::initialize();
    }
    
    public function index()
    {
        $form = dcFormCreate([
            'name'         => 'adminDubIndex',
            'action'       => dcUrl('index/admin.dub/update'),
            'items'        => $this->formItems(config('dub.')),
            'reset'        => '重置',
            'class_button' => 'text-left',
        ]);

        $this->assign('form',$form);

        $this->assign($this->site);

        return $this->fetch('admin.dub/index');
    }

    public function update()
    {
        $post = dcArrayFilter(input('post.'), array_merge(array_keys($this->formItems()),['fields']));
        if(!$post){
            $this->error('参数错误');
        }
        //合并初始值
        $post = array_merge($this->defaults, $post);
        //保存配置
        $this->configSave('./config/dub.php', $post, 'index/admin.dub/index');
    }
    
    private function formItems($data=[])
    {
        return [
            'name' => [
                'type'        => 'text',
                'value'       => $data['name'],
                'required'    => true,
                'title'       => '应用名称',
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
            'score' => [
                'type'        => 'number',
                'value'       => $data['score'],
                'required'    => true,
                'title'       => '所需积分',
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
            'icon' => [
                'type'        => 'text',
                'value'       => $data['icon'],
                'required'    => true,
                'title'       => '应用图标',
                'placeholder' => 'iconfont icon-sad',
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
            'fields[text][rows]' => [
                'type'        => 'number',
                'value'       => $data['fields']['text']['rows'],
                'required'    => true,
                'title'       => '表单行数',
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
            'fields[text][tips]' => [
                'type'        => 'text',
                'value'       => $data['fields']['text']['tips'],
                'required'    => true,
                'title'       => '表单提示',
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
            'title' => [
                'type'        => 'text',
                'value'       => $data['title'],
                'required'    => true,
                'title'       => '标题',
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
            'keywords' => [
                'type'        => 'text',
                'value'       => $data['keywords'],
                'required'    => true,
                'title'       => '关键字',
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
            'description' => [
                'type'        => 'textarea',
                'value'       => $data['description'],
                'required'    => true,
                'title'       => '描述',
                'rows'        => 3,
                'class_row'   => 'mb-3',
                'class_label' => 'form-label',
            ],
        ];
    }
}